/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int pathSum(TreeNode* root, int targetSum) {
        int ans=0;
        unordered_map<long long,int> cnt{{0,1}};

        auto dfs=[&](this auto&& dfs,TreeNode* node,long long sum)
        {
            if(node==nullptr) return;

            sum+=node->val;
            //node为路径终点，统计有多少起点

            ans+=cnt[sum-targetSum];

            cnt[sum]++;

            dfs(node->left,sum);

            dfs(node->right,sum);
            cnt[sum]--;
        };
        dfs(root,0);
        return ans;
    }
};
//思路可以类比前缀和为k，即560
